<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit\Ctl;

use Srv\Apps\Apis\Common\Ctl\StorageCtl;
use Srv\Apps\Apis\Common\DefCtl\DefStorageCategoryCtl;
use Srv\Apps\Proto\DTO\Skit\SkitInfoAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitInfoEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitInfoExtItemDTO;
use Srv\Apps\Proto\DTO\Skit\SkitInfoListDTO;
use Srv\Apps\Proto\Enum\YesOrNoEnum;
use Srv\Apps\Proto\VO\Skit\SkitCategoryVO;
use Srv\Apps\Proto\VO\Skit\SkitInfoListVO;
use Srv\Apps\Proto\VO\Skit\SkitInfoVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommJson;
use Srv\Libs\Common\CommString;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\AuthData;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;

final class SkitInfoCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param int $skioId
     * @return array
     * getSkitInfoSrc
     */
    public function getSkitInfoSrc(int $skioId):array
    {
        if($skioId < 1) return [];
        $isFail     = false;
        $info       = $this->DbAuto->getDataOne('SELECT skio_id, skio_key, skio_name, skio_desc, skio_cover, skio_channel, skcy_id, sklb_ids, skio_finish, skio_issue, skio_vip_unlock, skio_coin_unlock, skio_ad_unlock, skio_ad_unlock_num, skio_ad_unlock_limit, skio_ad_unlock_day_limit, skio_share_unlock, skio_share_unlock_num, skio_share_unlock_limit, skio_batch_unlock_discount, skio_batch_unlock_desc,  skio_limit_series, skio_total_num, skio_real_num, skio_chasing_user, skio_chasing_user_basic, skio_play_num, skio_like_user, skio_create_time, skio_update_time FROM '.$this->DbAuto->getTableNameAll('skit_info').' WHERE skio_id='.$skioId.' AND skio_delete=0', $isFail);
        if($isFail || !isset($info['skio_id'])) return [];
        return $info;
    }

    /**
     * @param array $skioIdList
     * @return array
     * getSkitInfoSrcBatch
     */
    public function getSkitInfoSrcBatch(array $skioIdList):array
    {
        if(count($skioIdList) < 1) return [];
        $isFail     = false;
        $list       = $this->DbAuto->getData('SELECT skio_id, skio_key, skio_name, skio_desc, skio_cover, skio_channel, skcy_id, sklb_ids, skio_finish, skio_issue, skio_vip_unlock, skio_coin_unlock, skio_ad_unlock, skio_ad_unlock_num, skio_ad_unlock_limit, skio_ad_unlock_day_limit, skio_share_unlock, skio_share_unlock_num, skio_share_unlock_limit, skio_batch_unlock_discount, skio_batch_unlock_desc,  skio_limit_series, skio_total_num, skio_real_num, skio_chasing_user, skio_chasing_user_basic, skio_play_num, skio_like_user, skio_create_time, skio_update_time FROM '.$this->DbAuto->getTableNameAll('skit_info').' WHERE skio_id IN('.implode(',', $skioIdList).') AND skio_delete=0', $isFail);
        if($isFail || count($list) < 1) return [];
        $listData   = [];
        foreach ($list as $item){
            $skioIdItem             = intval($item['skio_id']??0);
            if($skioIdItem < 1) continue;
            $listData[$skioIdItem]  = $item;
        }
        return $listData;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return SkitInfoListVO|null
     * getSkitInfoList
     */
    public function getSkitInfoList(Page $Page, array $whereData): ?SkitInfoListVO
    {
        $limitString            = $Page->getLimitString();
        $whereString            = ' WHERE skio_delete=0 ';
        // 检索条件
        if(count($whereData) > 0)foreach ($whereData as $key => $val){
            switch ($key){
                case SkitInfoListDTO::_skioId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_id='. intval($val);
                    break;
                case SkitInfoListDTO::_skioIdList:
                    if(!CommArray::isForeach($val)) return null;
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_id IN ('.implode(',', $val).')';
                    break;
                case SkitInfoListDTO::_skcyId:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skcy_id='. intval($val);
                    break;
                case SkitInfoListDTO::_infoKey:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_key LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitInfoListDTO::_name:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_name LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    break;
                case SkitInfoListDTO::_issue:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_issue='. intval($val);
                    break;
                case SkitInfoListDTO::_finish:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_finish='. intval($val);
                    break;
                case SkitInfoListDTO::_vipUnlock:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_vip_unlock='. intval($val);
                    break;
                case SkitInfoListDTO::_coinUnlock:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_coin_unlock='. intval($val);
                    break;
                case SkitInfoListDTO::_adUnlock:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_ad_unlock='. intval($val);
                    break;
                case SkitInfoListDTO::_channel:
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skio_channel='. intval($val);
                    break;
                case SkitInfoListDTO::_sklbId:
                    $sklbId         = intval($val);
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'JSON_CONTAINS(sklb_ids, \''.$sklbId.'\')' ;
                    break;
                case SkitInfoListDTO::_categoryName:
                    $tempSql        = 'SELECT skcy_id FROM '.$this->DbAuto->getTableNameAll('skit_category').' WHERE skcy_name=\''.$val.'\' AND skcy_delete=0';
                    $whereString    .= (strlen($whereString)>0?' AND ':'').'skcy_id IN('.$tempSql.')';
                    break;
            }
        }
        $isFail                 = false;
        $sql                    = 'SELECT SQL_CALC_FOUND_ROWS skio_id, skio_key, skio_name, skio_desc, skio_cover, skio_channel, skcy_id, sklb_ids, skio_finish, skio_issue, skio_vip_unlock, skio_coin_unlock, skio_ad_unlock, skio_ad_unlock_num, skio_ad_unlock_limit, skio_ad_unlock_day_limit, skio_share_unlock, skio_share_unlock_num, skio_share_unlock_limit, skio_batch_unlock_discount, skio_batch_unlock_desc,  skio_limit_series, skio_total_num, skio_real_num, skio_chasing_user, skio_chasing_user_basic, skio_play_num, skio_like_user, skio_create_time, skio_update_time FROM '.$this->DbAuto->getTableNameAll('skit_info').$whereString.' ORDER BY skio_id DESC '.$limitString;
        $list                   = $this->DbAuto->getData($sql, $isFail);
        $totalNum               = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $SkitInfoVOList         = [];
        if(CommArray::isForeach($list)){
            $skcyIdList             = array_unique(array_map('intval', array_column($list, 'skcy_id')));
            $SkitCategoryVOList     = SkitCategoryCtl::tag()->getSkitCategoryBatch($skcyIdList);
            foreach($list as $info){
                $skioIdItem         = intval($info['skio_id']);
                if($skioIdItem < 1) continue;
                $SkitInfoVO         = $this->getSkitInfoVO($info);
                if(!($SkitInfoVO instanceof SkitInfoVO)) continue;
                $SkitCategoryVO     = $SkitCategoryVOList[intval($info['skcy_id']??0)]??null;
                if($SkitCategoryVO instanceof SkitCategoryVO) $SkitInfoVO->setCategory($SkitCategoryVO);
                $SkitInfoVOList[]   = $SkitInfoVO;
            }
        }
        $SkitInfoListVO         = new SkitInfoListVO();
        $SkitInfoListVO->setList($SkitInfoVOList);
        $SkitInfoListVO->setPage($Page->getPageVO($totalNum));
        return $SkitInfoListVO;
    }

    /**
     * @param int $skioId
     * @return SkitInfoVO|null
     * getSkitInfoInfo
     */
    public function getSkitInfoInfo(int $skioId):?SkitInfoVO
    {
        $infoSrc        = $this->getSkitInfoSrc($skioId);
        if(!isset($infoSrc['skio_id'])) return null;
        $SkitInfoVO     = $this->getSkitInfoVO($infoSrc);
        if(!($SkitInfoVO instanceof SkitInfoVO)) return null;
        $SkitCategoryVO = SkitCategoryCtl::tag()->getSkitCategoryInfo(intval($infoSrc['skcy_id']??0));
        if($SkitCategoryVO instanceof SkitCategoryVO) $SkitInfoVO->setCategory($SkitCategoryVO);
        return $SkitInfoVO;
    }

    /**
     * @param array $skioIdList
     * @return array
     * getSkitInfoBatch
     */
    public function getSkitInfoBatch(array $skioIdList):array
    {
        $skitInfoList   = $this->getSkitInfoSrcBatch($skioIdList);
        $listData       = [];
        if(CommArray::isForeach($skitInfoList)){
            $skcyIdList                 = array_unique(array_map('intval', array_column($skitInfoList, 'skcy_id')));
            $SkitCategoryVOList         = SkitCategoryCtl::tag()->getSkitCategoryBatch($skcyIdList);
            foreach ($skitInfoList as $item){
                $skioIdItem             = intval($item['skio_id']??0);
                if($skioIdItem < 1) continue;
                $SkitInfoVO             = $this->getSkitInfoVO($item);
                if(!($SkitInfoVO instanceof SkitInfoVO)) continue;
                $SkitCategoryVO         = $SkitCategoryVOList[intval($item['skcy_id']??0)]??null;
                if($SkitCategoryVO instanceof SkitCategoryVO) $SkitInfoVO->setCategory($SkitCategoryVO);
                $listData[$skioIdItem]  = $SkitInfoVO;
            }
        }
        return $listData;
    }

    /**
     * @param array $info
     * @return SkitInfoVO|null
     * getSkitInfoVO
     */
    public function getSkitInfoVO(array $info):?SkitInfoVO
    {
        $skioId             = intval($info['skio_id']??0);
        if($skioId < 1) return null;
        $SkitInfoVO         = new SkitInfoVO();
        $SkitInfoVO->setSkioId($skioId);
        $SkitInfoVO->setKey(trim($info['skio_key']));
        $SkitInfoVO->setName(trim($info['skio_name']));
        $SkitInfoVO->setDesc(trim($info['skio_desc']));
        $SkitInfoVO->setCover(StorageCtl::tag()->getStorageUrl(trim($info['skio_cover']), true));
        $SkitInfoVO->setChannel(intval($info['skio_channel']));
        $sklbIdList         = CommJson::decodeArray($info['sklb_ids']);
        if(count($sklbIdList) > 0){
            $SkitLabelVOList= SkitLabelCtl::tag()->getSkitLabelInfoBatch($sklbIdList);
            if(count($SkitLabelVOList) > 0) $SkitInfoVO->setLabelList($SkitLabelVOList);
        }
        $SkitInfoVO->setFinish(YesOrNoEnum::parseValue($info['skio_finish']));
        $SkitInfoVO->setIssue(YesOrNoEnum::parseValue($info['skio_issue']));
        $SkitInfoVO->setVipUnlock(YesOrNoEnum::parseValue($info['skio_vip_unlock']));
        $SkitInfoVO->setCoinUnlock(YesOrNoEnum::parseValue($info['skio_coin_unlock']));
        $SkitInfoVO->setAdUnlock(YesOrNoEnum::parseValue($info['skio_ad_unlock']));
        $SkitInfoVO->setAdUnlockNum(intval($info['skio_ad_unlock_num']));
        $SkitInfoVO->setAdUnlockLimit(intval($info['skio_ad_unlock_limit']));
        $SkitInfoVO->setShareUnlock(YesOrNoEnum::parseValue($info['skio_share_unlock']));
        $SkitInfoVO->setShareUnlockNum(intval($info['skio_share_unlock_num']));
        $SkitInfoVO->setShareUnlockLimit(intval($info['skio_share_unlock_limit']));
        $SkitInfoVO->setBatchUnlockDiscount(intval($info['skio_batch_unlock_discount']));
        $SkitInfoVO->setBatchUnlockDesc(trim($info['skio_batch_unlock_desc']));
        $SkitInfoVO->setLimitSeries(intval($info['skio_limit_series']));
        $SkitInfoVO->setTotalNum(intval($info['skio_total_num']));
        $SkitInfoVO->setRealNum(intval($info['skio_real_num']));
        $SkitInfoVO->setChasingUser(intval($info['skio_chasing_user']));
        $SkitInfoVO->setChasingUserBasic(intval($info['skio_chasing_user_basic']));
        $SkitInfoVO->setPlayNum(intval($info['skio_play_num']));
        $SkitInfoVO->setLikeUser(intval($info['skio_like_user']));
        $SkitInfoVO->setCreateTime(intval($info['skio_create_time']));
        $SkitInfoVO->setUpdateTime(intval($info['skio_update_time']));
        $SkitInfoVO->setAdUnlockDayLimit(intval($info['skio_ad_unlock_day_limit']));
        return $SkitInfoVO;
    }

    /**
     * @param $setData
     * @return int
     * skitInfoAdd
     */
    public function skitInfoAdd($setData):int
    {
        if(count($setData) < 1) return 0;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitInfoAddDTO::_name:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitInfoAddDTO::_desc:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_desc=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitInfoAddDTO::_coverKey:
                    $dsacId         = DefStorageCategoryCtl::tag()->getIdByKey(DefStorageCategoryCtl::KEY_VIDEO_COVER);
                    if($dsacId < 1) return 0;
                    $saveUrl        = StorageCtl::tag()->getStorageUrlRecord(AuthData::getLoginUserId(), $dsacId, $val);
                    if(strlen($saveUrl) < 1) return 0;
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_cover=\''.$this->DbAuto->getRealEscapeString($saveUrl).'\'';
                    break;
                case SkitInfoAddDTO::_channel:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_channel='.intval($val);
                    break;
                case SkitInfoAddDTO::_skcyId:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcy_id='.intval($val);
                    break;
                case SkitInfoAddDTO::_sklbIdList:
                    $sklbIds        = CommJson::encodeArray($val);
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sklb_ids=\''.$this->DbAuto->getRealEscapeString($sklbIds).'\'';
                    break;
                case SkitInfoAddDTO::_finish:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_finish='.intval($val);
                    break;
                case SkitInfoAddDTO::_issue:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_issue='.intval($val);
                    break;
                case SkitInfoAddDTO::_vipUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_vip_unlock='.intval($val);
                    break;
                case SkitInfoAddDTO::_coinUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_coin_unlock='.intval($val);
                    break;
                case SkitInfoAddDTO::_adUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock='.intval($val);
                    break;
                case SkitInfoAddDTO::_adUnlockNum:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock_num='.intval($val);
                    break;
                case SkitInfoAddDTO::_limitSeries:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_limit_series='.intval($val);
                    break;
                case SkitInfoAddDTO::_adUnlockLimit:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock_limit='.intval($val);
                    break;
                case SkitInfoAddDTO::_batchUnlockDiscount:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_batch_unlock_discount='.intval($val);
                    break;
                case SkitInfoAddDTO::_batchUnlockDesc:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_batch_unlock_desc=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitInfoAddDTO::_shareUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_share_unlock='.intval($val);
                    break;
                case SkitInfoAddDTO::_shareUnlockNum:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_share_unlock_num='.intval($val);
                    break;
                case SkitInfoAddDTO::_shareUnlockLimit:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_share_unlock_limit='.intval($val);
                    break;
                case SkitInfoAddDTO::_adUnlockDayLimit:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock_day_limit='.intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return 0;
        $currTime   = CommTime::getTimeStamp(0);
        $key        = $this->getSkitInfoKey();
        $isFail     = false;
        $sql        = 'INSERT INTO '.$this->DbAuto->getTableNameAll('skit_info').' SET '.$setString.', skio_key=\''.$this->DbAuto->getRealEscapeString($key).'\', skio_delete=0, skio_create_time='.$currTime.', skio_update_time='.$currTime;
        $skioId     = $this->DbAuto->getDataId($sql, $isFail);
        if($isFail || $skioId < 1) return 0;
        return $skioId;
    }

    /**
     * @param int $skioId
     * @param array $setData
     * @return bool
     * skitInfoEdit
     */
    public function skitInfoEdit(int $skioId, array $setData):bool
    {
        if($skioId < 1 || count($setData) < 1) return false;
        $setString      = '';
        foreach ($setData as $key => $val){
            switch ($key){
                case SkitInfoEditDTO::_name:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_name=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitInfoEditDTO::_desc:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_desc=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitInfoEditDTO::_coverKey:
                    $dsacId         = DefStorageCategoryCtl::tag()->getIdByKey(DefStorageCategoryCtl::KEY_VIDEO_COVER);
                    if($dsacId < 1) return false;
                    $saveUrl        = StorageCtl::tag()->getStorageUrlRecord(AuthData::getLoginUserId(), $dsacId, $val);
                    if(strlen($saveUrl) < 1) return false;
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_cover=\''.$this->DbAuto->getRealEscapeString($saveUrl).'\'';
                    break;
                case SkitInfoEditDTO::_channel:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_channel='.intval($val);
                    break;
                case SkitInfoEditDTO::_skcyId:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skcy_id='.intval($val);
                    break;
                case SkitInfoEditDTO::_sklbIdList:
                    $sklbIds        = CommJson::encodeArray($val);
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'sklb_ids=\''.$this->DbAuto->getRealEscapeString($sklbIds).'\'';
                    break;
                case SkitInfoEditDTO::_finish:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_finish='.intval($val);
                    break;
                case SkitInfoEditDTO::_issue:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_issue='.intval($val);
                    break;
                case SkitInfoEditDTO::_vipUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_vip_unlock='.intval($val);
                    break;
                case SkitInfoEditDTO::_coinUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_coin_unlock='.intval($val);
                    break;
                case SkitInfoEditDTO::_adUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock='.intval($val);
                    break;
                case SkitInfoEditDTO::_adUnlockNum:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock_num='.intval($val);
                    break;
                case SkitInfoEditDTO::_limitSeries:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_limit_series='.intval($val);
                    break;
                case SkitInfoEditDTO::_adUnlockLimit:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock_limit='.intval($val);
                    break;
                case SkitInfoEditDTO::_batchUnlockDiscount:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_batch_unlock_discount='.intval($val);
                    break;
                case SkitInfoEditDTO::_batchUnlockDesc:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_batch_unlock_desc=\''.$this->DbAuto->getRealEscapeString($val).'\'';
                    break;
                case SkitInfoEditDTO::_shareUnlock:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_share_unlock='.intval($val);
                    break;
                case SkitInfoEditDTO::_shareUnlockNum:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_share_unlock_num='.intval($val);
                    break;
                case SkitInfoEditDTO::_shareUnlockLimit:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_share_unlock_limit='.intval($val);
                    break;
                case SkitInfoEditDTO::_adUnlockDayLimit:
                    $setString      .= (strlen($setString) > 0 ? ', ' : '') . 'skio_ad_unlock_day_limit='.intval($val);
                    break;
            }
        }
        if(strlen($setString) < 1) return false;
        $isFail     = false;
        $currTime   = CommTime::getTimeStamp(0);
        $sql        = 'UPDATE '.$this->DbAuto->getTableNameAll('skit_info').' SET '.$setString.', skio_update_time='.$currTime.' WHERE skio_id='.$skioId.' AND skio_delete=0';
        if($this->DbAuto->getDataNum($sql, $isFail) < 1 || $isFail) return false;
        return true;
    }

    /**
     * @return string
     * getSkitInfoKey
     */
    public function getSkitInfoKey():string
    {
        $minLength          = 8;        // 最小长度
        $maxLength          = 16;       // 最大长度
        $maxNum             = 200;
        $currNum            = 1;
        $indexNum           = 1;
        $skitInfoKey        = strtolower(CommString::genRandStr($minLength));
        while($currNum <= $maxNum){
            $skioIdFind     = $this->DbAuto->getDataInt('SELECT skio_id FROM '.$this->DbAuto->getTableNameAll('skit_info').' WHERE binary skio_key=\''.$this->DbAuto->getRealEscapeString($skitInfoKey).'\'', 'skio_id');
            if($skioIdFind < 1) break;
            if($indexNum >= 50){                // 失败自增长度1
                $indexNum   = 0;
                $minLength  += 1;
            }
            if($minLength > $maxLength) break;  // 长度封顶
            $skitInfoKey    = strtolower(CommString::genRandStr($minLength));
            ++$indexNum;
            ++$currNum;
        }
        return $skitInfoKey;
    }

    /**
     * @param array $setData
     * @param array $oldData
     * @return bool
     * checkExtData
     */
    public function checkExtData(array $setData, array &$oldData):bool
    {
        if(count($setData) < 1) return false;
        $skioIdList         = [];
        foreach ($setData as $item){
            if(!isset($item[SkitInfoExtItemDTO::_skioId]) || !isset($item[SkitInfoExtItemDTO::_chasingUserBasic]) || intval($item[SkitInfoExtItemDTO::_skioId]) < 1) return false;
            $skioIdList[]   = intval($item[SkitInfoExtItemDTO::_skioId]);
        }
        $skioIdListSize     = count($skioIdList);
        $oldDataOri         = $this->getSkitInfoSrcBatch($skioIdList);
        if(count($oldDataOri) !== $skioIdListSize) return false;
        foreach ($skioIdList as $skioIdItem){
            if(!isset($oldDataOri[$skioIdItem])) return false;
            $oldData[]      = ['skioId' => $skioIdItem, SkitInfoExtItemDTO::_chasingUserBasic => intval($oldDataOri[$skioIdItem]['skio_chasing_user_basic'])];
        }
        if(count($oldData) !== $skioIdListSize) return false;
        return true;
    }

    /**
     * @param array $setData
     * @return bool
     * skitInfoExtEdit
     */
    public function skitInfoExtEdit(array $setData):bool
    {
        if(count($setData) < 1) return false;
        $setString      = '';
        $skioIdList     = [];
        foreach ($setData as $item){
            $skioIdItem             = intval($item[SkitInfoExtItemDTO::_skioId]??0);
            if($skioIdItem < 1) return false;
            $chasingUserBasicItem   = intval($item[SkitInfoExtItemDTO::_chasingUserBasic]??0);
            $setString              .= ' WHEN '.$skioIdItem.' THEN '.$chasingUserBasicItem;
            $skioIdList[]           = $skioIdItem;
        }
        if(strlen($setString) < 1 || count($skioIdList) < 1) return false;
        $currTime       = CommTime::getTimeStamp();
        $sql            = 'UPDATE '.$this->DbAuto->getTableNameAll('skit_info').' SET skio_chasing_user_basic=CASE skio_id '.$setString.' END, skio_update_time='.$currTime.' WHERE skio_id IN('.implode(',', $skioIdList).') AND skio_delete=0';
        return $this->DbAuto->getDataNum($sql)>0;
    }
}